<?php
/**
 * Logo_Controller
 * - Controller for module page Logo
 * @author ANHTAM
 * @since 1.1 - Sep 27, 2011
 * $Id: $
 */
// Require module for controller
require_once(LIB_DAO_DIR . "/MLogo.class.php");
require_once(LIB_CORE_DIR . "/Paging.class.php");

class Logo_Controller extends Controller {
	/**
     * - Constructor
     */
    public function __construct() {
    	parent::__construct();
    	$this->flagMenuImage = true;
    	$this->langue = true;
    	$this->getSession = Session::Get("USER");
    	if ($this->getSession['langue'] == 'en') {
    		$this->langue = false;
    	}
    }
    
    /**
     * actionIndex
     * - page index of controller
     */
    public function actionIndex() {
    	// Process init
        $objView = new View ();
        $this->tpl_title = 'Danh sách Logo';
        $this->tpl_title_list = 'Quản lý hình ảnh -> Quản lý Logo';
        
    	if (count ($this->getSession) <= 0) {
        	$this->sendRedirect();
        }
        
        $ojbLogo = new MLogo();
    	if ($this->isPost()) {
        	$dataPost = FunctionCommon::getPost(array('item', 'mode', 'type', 'checkdel'));
	        
	        // Delete by id
	        if ($dataPost['mode'] == 'delete') {
	        	$dataLogo = $ojbLogo->getByPrimaryKey($dataPost['item']);
	        	$ojbLogo->setAttribute('id', $dataPost['item']);
	        	$ojbLogo->delete();
	        	FunctionCommon::move(HTDOC_IMAGES_DIR."/logo/".$dataLogo['code']);
	        }
	        
	        // Delete all by array id
	    	if ($dataPost['mode'] == 'deleteall') {
	    		$arrPost = $dataPost['checkdel'];
	    		if (count ($arrPost) > 0) {
	    			foreach ($arrPost as $aP) {
	    				$dataLogo = $ojbLogo->getByPrimaryKey($aP);
	        			$ojbLogo->setAttribute('id', $aP);
	        			$ojbLogo->delete();
	        			FunctionCommon::move(HTDOC_IMAGES_DIR."/logo/".$dataLogo['code']);
	    			}
	    		}
	        }
	        
	        // Up
	        if ($dataPost['mode'] == 'up') {
	        	$id = $dataPost['item'];
	        	$seq = $dataPost['type'];
	        	$seqMin = $ojbLogo->getSeq(array("seq"=>array("math"=>">", "value"=>$seq)), false, true);
	        	$idMin = $ojbLogo->getIdBySeq($seqMin);
	        	
	        	$ojbLogo->setAttribute('seq', $seqMin);
	        	$ojbLogo->update(array('id' => $id));
	        	$ojbLogo = new MLogo();
	        	$ojbLogo->setAttribute('seq', $seq);
	        	$ojbLogo->update(array('id' => $idMin));
	        }
	        
	    	// Down
	        if ($dataPost['mode'] == 'down') {
	        	$id = $dataPost['item'];
	        	$seq = $dataPost['type'];
	        	$seqMax = $ojbLogo->getSeq(array("seq"=>array("math"=>"<", "value"=>$seq)), true);
	        	$idMax = $ojbLogo->getIdBySeq($seqMax);
	        	
	        	$ojbLogo->setAttribute('seq', $seqMax);
	        	$ojbLogo->update(array('id' => $id));
	        	$ojbLogo = new MLogo();
	        	$ojbLogo->setAttribute('seq', $seq);
	        	$ojbLogo->update(array('id' => $idMax));
	        }
        }
        
        $this->maxseq = $ojbLogo->getSeq(array(), true);
        $this->minseq = $ojbLogo->getSeq(array(), false, true);
        
    	// List all
    	$arrParam = array('m' => 'logo');
    	$arrField = array('id', 'seq', 'code', 'url');
        $condition = array();
		
        $data = $this->paging($arrParam, $arrField, $condition);
        $arrContent = $data["data"];
		$this->arrContent = array();
		if (count ($arrContent) > 0) {
			foreach ($arrContent as $content) {
				$this->arrContent[] = $content;
			}
			$this->currPage = $data["currPage"];
			$this->numberPage = $data["numberPage"];
			$this->strPaging = $data["strPaging"];
		}        

        // Process view
        $objView->assignobj($this);
        $objView->display(false);
    }
    
	/**
     * actionEdit
     * - page edit of controller
     */
    public function actionEdit() {
    	
    	// Process init
        $objView = new View ();
        $this->tpl_title = 'Sửa tên danh mục';
        $this->tpl_title_list = 'Quản lý hình ảnh -> Quản lý Logo Add/Edit';
        $this->tpl_button = "Thêm mới";
        $this->arrField = array();
        
    	if (count ($this->getSession) <= 0) {
        	$this->sendRedirect();
        }
        
    	$ojbLogo = new MLogo();
    	
    	if ($this->isPost()) {
        	$arrPost = array('url', 'diachi', 'address', 'nameimage', 'item', 'mode', 'type');
        	$arrPostValue = FunctionCommon::getPost($arrPost);
        	
        	$this->id = $arrPostValue['item'];
        	
        	if ($arrPostValue['mode'] != '') {
        		
        		// Update du lieu
        		if ($arrPostValue['type'] == 'edit' && $this->id != '') {
        			$ojbLogo->setAttribute('code', $arrPostValue['nameimage']);
        			$ojbLogo->setAttribute('url', $arrPostValue['url']);
        			if ($this->langue) {
	        			$ojbLogo->setAttribute('diachi', $arrPostValue['diachi']);
        			} else {
			        	$ojbLogo->setAttribute('address', $arrPostValue['address']);
        			}
		        	$ojbLogo->update(array('id' => $this->id));
		        	$this->sendRedirect("index.php?m=logo");
        		}
        		
        		// Insert du lieu
	        	if ($arrPostValue['type'] == 'edit' && $this->id == '') {
		    		$ojbLogo->setAttribute('seq', $ojbLogo->getSeq(array(), true) + 1);
		    		$ojbLogo->setAttribute('code', $arrPostValue['nameimage']);
		    		$ojbLogo->setAttribute('url', $arrPostValue['url']);
		    		if ($this->langue) {
			        	$ojbLogo->setAttribute('diachi', $arrPostValue['diachi']);
		    		} else {
			        	$ojbLogo->setAttribute('address', $arrPostValue['address']);
		    		}
		        	$ojbLogo->insert();
		        	$this->sendRedirect("index.php?m=logo");
		        }
        		
        		// Xu ly file
	        	if ($arrPostValue['type'] == 'upload') {
		        	if (isset ($_FILES["code"]["name"]) && $_FILES["code"]["name"] != '') {
			        	$this->tenFile = date("dHis")."_".$_FILES["code"]["name"];
			        	if (Validator::validateTypeImage($this->tenFile)) {
			        		if (FunctionCommon::uploadFileTo($_FILES["code"]["tmp_name"], HTDOC_IMAGES_DIR."/logo", $this->tenFile)) {
			        			// Delete image
			        			FunctionCommon::move(HTDOC_IMAGES_DIR."/logo/".$arrPostValue['nameimage']);
			        		}
			        	}
		        	} else {
		        		$this->tenFile = $arrPostValue['nameimage'];
		        	}
		        }
		        
		        // Delete image
	        	if ($arrPostValue['type'] == 'delete') {
		        	$tenFile = $arrPostValue['nameimage'];
		        	FunctionCommon::move(HTDOC_IMAGES_DIR."/logo/".$tenFile);
		        }
		        
		        $this->arrField = $arrPostValue;
        	} else {
        		$this->tpl_title = 'Sửa Logo';
        		$this->tpl_button = "Sửa";
	        	$this->arrField = $ojbLogo->getByPrimaryKey($this->id);
	        	$this->tenFile = $this->arrField["code"];
        	}
    	}

        // Process view
        $objView->assignobj($this);
        $objView->display(false);
    }
    
	/**
    * paging
    * - Array $arrParam
    * - Array $condition
    */
	public function paging($arrParam = null, $arrField = array(), $condition = array()) {
		$objPaging = new Paging();
		$ojbLogo = new MLogo();
	    // Get total record of content menu
		$totalRecord = $ojbLogo->getTotalData($condition);
		 // Get page current
        if (isset($_REQUEST["pageNo"])) {
        	$currentPage = $_REQUEST["pageNo"];
        }
        else {
            $currentPage = 1;
        }
		$numberRecord = 5;
        // get page number
		if (!is_null($numberRecord) && $numberRecord != 0) {
			$numberPage = ceil($totalRecord / $numberRecord);
		} else {
			$numberPage = 0;
		}
		// get start record
		$startRecord = ($currentPage - 1) * $numberRecord;

        // get array paging
		$arrPage = $objPaging->getArrPaging($currentPage, $numberPage);
		// get string page
		$strPaging = $objPaging->getStrPaging($arrPage, $currentPage, $numberPage, $arrParam);

        // Get all content menu
        $content = $ojbLogo->getListData($startRecord, $numberRecord, $arrField, $condition);
        $no = $startRecord + 1;
		$result = array("total"=>$totalRecord,
						"data"=>$content,
						"currPage"=>$currentPage,
						"numberPage"=>$numberPage,
						"strPaging"=>$strPaging,
						"no"=>$no);

		return $result;
	}
}
?>

